import { login, getUserInfo, logout, jscode2session, memberInfo, coachInfo } from '@/api/userApi'
import { getStore, setStore } from '@/util/service'
const user = {
  namespaced: true,
  state: {
    userInfo: getStore('userInfo') || {},
    role: getStore('role') || '[]',
    uniToken: getStore('uniToken') || '',
    menu: getStore('menu') || [],
    permission: {},
    openId: ''
  },
  getters: {},
  mutations: {
    SET_TOKEN: (state, params) => {
      state.uniToken = params
      setStore('uniToken', params)
    },
    SET_MENU: (state, params) => {
      state.menu = params
      setStore('menu', params)
    },
    SET_ROLE: (state, params) => {
      state.role = JSON.stringify(params)
      setStore('role', JSON.stringify(params))
    },
    SET_USER_INFO: (state, params) => {
      state.userInfo = params
      setStore('userInfo', params)
    },
    SET_OPENID: (state, params) => {
      state.openId = params
    }
  },
  actions: {
    Login({ state, commit, dispatch }, params) {
      return new Promise((resolve, reject) => {
        login(params).then(res => {
          // console.log(res)
          if (res.code == 200) {
            commit('SET_TOKEN', res.data.loginRes.accessToken.value)
            if(res.data.loginRes.ext){
              commit('SET_ROLE', res.data.loginRes.ext.roles)
            } else {
              commit('SET_ROLE', [])
            }
          }
          resolve(res)
        })
      })
    },
    Logout({ state, commit, dispatch }, params) {
      return new Promise((resolve, reject) => {
        logout(params).then(res => {
          commit('SET_TOKEN', '')
          commit('SET_USER_INFO', {})
          resolve(res)
        })
      })
    },
    GetUserInfo({ state, commit, dispatch }, params) {
      return new Promise((resolve, reject) => {
        getUserInfo(params).then(res => {
          commit('SET_ROLE', res.data.role)
          commit('SET_USER_INFO', res.data)
          resolve(res)
        })
      })
    },
    GetMenu({ state, commit, dispatch }, params) {
      return new Promise((resolve, reject) => {
        var role = state.role.length > 0 ? state.role[0] : ''
        getMenu(role).then(res => {
          commit('SET_MENU', res.data)
          resolve(res)
        })
      })
    },
    // 将菜单列表扁平化形成权限列表
    GetPermissionList({ state, dispatch }) {
      return new Promise((resolve) => {
        let permissionList = []
        // 将菜单数据扁平化为一级
        function flatNavList(arr) {
          for (let v of arr) {
            if (v.children && v.children.length) {
              flatNavList(v.children)
            } else {
              permissionList.push(v)
            }
          }
        }
        // dispatch('GetUserInfo', state.token).then(res => {
        dispatch('GetMenu').then(res => {
          flatNavList(state.menu)
          resolve(permissionList)
        })
        // })
      })
    },
    Jscode2session({ state, commit, dispatch }, params) {
      return new Promise((resolve, reject) => {
        jscode2session(params).then(res => {
          resolve(res)
        })
      })
    },
    MemberInfo({ state, commit, dispatch }, params) {
      return new Promise((resolve, reject) => {
        memberInfo(params).then(res => {
          commit('SET_USER_INFO', res.data)
          resolve(res)
        })
      })
    },
    CoachInfo({ state, commit, dispatch }, params) {
      return new Promise((resolve, reject) => {
        coachInfo(params).then(res => {
          commit('SET_USER_INFO', res.data)
          resolve(res)
        })
      })
    }
  }
}
export default user
